<?php
class youkaControl extends skymvc{
	
	public function __construct(){
		parent::__construct();	
	}
	
	public function onDefault(){
		$where=" 1=1 ";
		$url=APPADMIN."?m=youka&a=default";
		$limit=20;
		$start=get("per_page","i");
		$nickname=get_post('nickname','h');
		if($nickname){
			$u=M("user")->selectRow(" nickname='".$nickname."' ");
			if($u){
				$where.=" AND userid=".$u['userid'];				
			}else{
				$where.=" AND 1=2 ";
			}
		}
		$option=array(
			"start"=>intval(get_post('per_page')),
			"limit"=>$limit,
			"order"=>" id DESC",
			"where"=>$where
		);
		$rscount=true;
		$data=M("youka")->select($option,$rscount);
		if($data){
			foreach($data as $v){
				$uids[]=$v['userid'];
				$ssids[]=$v['sushe'];
			}
			$us=M("user")->getUserByIds($uids);
			$sushes=M("sushe")->getListByIds($ssids);
			foreach($data as $k=>$v){
				$v['nickname']=$us[$v['userid']]['nickname'];
				$v['telephone']=$us[$v['userid']]['telephone'];
				if($v['sushe']){
					$v['ss_name']=$sushes[$v['sushe']]['title'];
				}
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(
			array(
				"data"=>$data,
				"pagelist"=>$pagelist,
				"rscount"=>$rscount,
				"url"=>$url
			)
		);
		$this->smarty->display("youka/index.html");	
	}
	
	public function onAdd(){
		$id=get_post('id','i');
		$row=M("youka")->selectRow("id=".$id);
		if($row){
			$user=M("user")->selectRow("userid=".$row['userid']);
		}
		$this->smarty->assign(array(
			"data"=>$row,
			"user"=>$user
		));
		$this->smarty->display("youka/add.html");	
	}
	
	public function onSave(){
		$id=get_post('id','i');
		$row=M("youka")->selectRow("id=".$id);
		$money=post('money','r',2);
		$data['sxmoney']=$row['sxmoney']+$money;
		$data['money']=$row['money']+$money;
		M("youka")->update($data,"id=".$id);
		$msg=array(
			"dateline"=>time(),
			"type_id"=>1,
			"siteid"=>SITEID,	
			"status"=>0,
			"userid"=>$row['userid'],
			"content"=>"您的优卡额度变更了".$money."元，现在".$data['sxmoney']."元",
		);
		M("notice")->insert($msg);
		$log=array(
			"dateline"=>time(),
			"typeid"=>3,
			"siteid"=>SITEID,	
			
			"userid"=>$row['userid'],
			"content"=>"您的优卡额度变更了".$money."元，现在".$data['sxmoney']."元",
		);
		m("youka_log")->insert($log);
		$this->goAll("保存成功");
	}
	
}

?>